package data.hierarchy;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import data.base.Vertex;
import data.complex.Boundary;
import data.complex.Triangle;

public class Mesh implements Iterable<TriangleGroup>, IVertexProvider{
  private List<TriangleGroup> triangleGroups;
  private List<Vertex> pointsCache;
  private Boundary boundary;
  
  public Mesh(){
    triangleGroups = new ArrayList<TriangleGroup>();
  }
  
  public void addTriangleGroup(TriangleGroup tg){
    triangleGroups.add(tg);
  }

  @Override
  public Iterator<TriangleGroup> iterator() {
    return triangleGroups.iterator();
  }

  @Override
  public Iterable<Vertex> getVertices() {
    if(pointsCache == null){
      pointsCache = new ArrayList<Vertex>();
      for(TriangleGroup tg : this){
        for(Triangle t : tg){
          pointsCache.add(t.getA());
          pointsCache.add(t.getB());
          pointsCache.add(t.getC());
        }
      }
    }
    return pointsCache;
  }
  
  public Boundary getBoundary(){
    if(boundary == null){
      boundary = new Boundary(this);
    }
    return boundary;
  }
}
